<?php

/**
 * Created by wangchao.
 * User: Administrator
 * Date: 17-10-20
 * Time: 下午4:35
 */

namespace Admin\Controller;

class MsgController extends CommonController
{
    /**
     * 公告列表
     */
    public function msg_list()
    {
        $where ='status = 1';
        $list = M('Msg')->where($where)->order('msg_id desc') ->select();
        $this->assign('list', $list);


        $count   =  M('Msg')->where($where)->count();
        $p = getpage($count,20);
        $this->assign('page', $p->show()); // 赋值分页输出
        $this->assign('count',$count);
        $this->display('msg_list');
    }

    /**
     * 添加公告
     */
    public function add_msg()
    {
        if (IS_POST) {
            $title = I('post.title');
            $content = I('post.content');
            $data = [
                'title' => $title,
                'content' => $content,
                'create_time' => time(),
                'status' => 1,
            ];
            $res = M('Msg')->add($data);
            $this->success('添加成功！', U('Msg/msg_list'));
        }
        $this->display('add_msg');
    }

    /**
     * 修改公告
     */
    public function edit_msg()
    {
        if (IS_POST) {
            $post = I('post.');
            $msg_id = $post['msg_id'];
            $post['create_time'] = time();
            $res = M('Msg')->where(['msg_id' => $msg_id])->save($post);
            
            $this->success('修改成功！', U('Msg/msg_list'));
        }
        $msg_id = I('msg_id');
        $info = M('Msg')->where(['msg_id' => $msg_id])->find();
        $this->assign('info', $info);
        $this->display('edit_msg');
    }

    /**
     * 删除公告
     */
    public function del_msg()
    {
        if(IS_AJAX){
            $msg_id = I('msg_id');
            $res = M('Msg')->where(['msg_id' => $msg_id])->delete();
            $this->success('删除成功', U('Msg/msg_list')); 
        }
        
    }
    /**
     * 政策发布列表
     */
    public function policy_list(){
        $list = M('policy')->order('id desc')->select();
        $this->assign('list',$list);
        $count   =  M('policy')->count();
        $this->assign('count',$count);
        $p = getpage($count,20);
        $this->assign('page', $p->show()); // 赋值分页输出
        $this->display('policy_list');
    }
    /**
     * 添加政策
     */
    public function add_policy(){
        if(IS_POST){
            $data = array(
                'title' => I('title'),
                'content' => I('content'),
                'create_time' => time(),
            );
            $result = M('policy')->add($data);
            $this->success('添加成功',U('Msg/policy_list'));
        }
        $this->display();
    }
    /**
     * 编辑政策
     */
    public function edit_policy(){
        $id = I('id');
        if(IS_POST){
            $data = array(
                'title' => I('title'),
                'content' => I('content'),
            );
            $result = M('policy')->where(['id'=>$id])->save($data);
            $this->success('修改成功',U('Msg/policy_list'));
        }
        $info = M('policy')->where(['id' => $id])->find();
        $this->assign('info', $info);
        $this->display();
    }
    /**
     * 删除政策
     */
    public function del_policy(){
        if(IS_AJAX){
            $id = I('id');
            $result = M('policy')->where(['id'=>$id])->delete();
            $this->success('删除成功',U('Msg/policy_list'));
        }
        
    }
}
